//
//  LoginViewController.m
//  RONGAO
//
//  Created by wei wang on 2017/3/17.
//  Copyright © 2017年 wei wang. All rights reserved.
//

#import "LoginViewController.h"
#import "PublicLoginApi.h"
#import "JPushUploadDeviceTokenAPI.h"
@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;

@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;

@property (weak, nonatomic) IBOutlet UIButton *maskBtn;

@end

@implementation LoginViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
#pragma mark - 注册成功
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(registerSuccess:)
                                                 name:NOTIFY_REGISTER_SUCCESS
                                               object:nil];
    [_phoneTextField setValue:[UIColor colorWithHexString:@"B8B8B8"] forKeyPath:@"_placeholderLabel.textColor"];
    [_pwdTextField setValue:[UIColor colorWithHexString:@"B8B8B8"] forKeyPath:@"_placeholderLabel.textColor"];
    
    
}
- (IBAction)loginAction:(UIButton *)sender {
    
    
    if (![_phoneTextField.text isMobileNumber]) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号码.."];
        return;
    }
    
    if (![_pwdTextField.text isRegexPassword]) {
        [SVProgressHUD showErrorWithStatus:@"请输入6-20位字母或数字密码\n不包含除_@.之外的特殊符号"];
        return;
    }
    
    [self loginActionStart];

}
- (void)loginActionStart{
    
    [[DTCloudManager defaultJNI_iOS_SDK] loginWithUsername:_phoneTextField.text password:_pwdTextField.text successCallback:^(NSDictionary *dic) {
        
        [SVProgressHUD showSuccessWithStatus:@"登录成功"];
        SAVE_OBJECT(_phoneTextField.text, USERNAME);
        SAVE_OBJECT(_pwdTextField.text, PASSWORD);
        SAVE_OBJECT(dic[@"data"][@"uid"], DTUID);
        SAVE_OBJECT(dic[@"data"][@"token"], TOKEN);
        SAVE_BOOL(YES, LOGIN);
        [[AppDelegate globalDelegate] setRootViewController];
        
        if (EXRACT_OBJECT(RegistrationID) != nil) {
            
            JPushUploadDeviceTokenAPI *api = [[JPushUploadDeviceTokenAPI alloc] initWithToken:EXRACT_OBJECT(RegistrationID)];
            [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
                
                [api analyseResponseObject];
                
            } failure:^(YTKBaseRequest *request) {
                
                [SVProgressHUD showErrorWithStatus:@"网络错误"];
            }];
 
        }
        
        
    } errorCallback:^(NSDictionary *dic) {
        
        
        [SVProgressHUD showErrorWithStatus:dic[@"errmsg"]];
        
    }];
    
    
    /*
     PublicLoginApi *LoginApi = [[PublicLoginApi alloc]initWithDic:@{@"username":_phoneTextField.text,@"password":_pwdTextField.text}];
     
     
     [self startWithYTKRequest:LoginApi completionBlockWithSuccess:^(YTKBaseRequest *request) {
     NSDictionary *dic = [LoginApi responseJSONObject];
     
     [SVProgressHUD showSuccessWithStatus:@"登录成功"];
     SAVE_OBJECT(_phoneTextField.text, USERNAME);
     SAVE_OBJECT(_pwdTextField.text, PASSWORD);
     SAVE_OBJECT(dic[@"data"][@"uid"], DTUID);
     SAVE_OBJECT(dic[@"data"][@"token"], TOKEN);
     SAVE_BOOL(YES, LOGIN);
     [[AppDelegate globalDelegate] setRootViewController];
     
     } failure:nil];
     
     */
    
}

- (IBAction)maskAction:(UIButton *)sender {
    
    sender.selected = !sender.selected;
    self.pwdTextField.secureTextEntry = !sender.selected;
    
}

#pragma mark - 注册成功返回
- (void)registerSuccess:(NSNotification *)notification {
    
    NSDictionary * dict = notification.userInfo;
    dispatch_async(dispatch_get_main_queue(), ^{
        _phoneTextField.text = dict[@"account"];
        _pwdTextField.text = dict[@"password"];
    });
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
